package com.coollang.smartbra.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.model.UMoneyDetailsModel;
import com.coollang.smartbra.utils.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: Timmy
 * @Data: 2017/9/8
 * @Fuction:
 */
public class UMoneyDetailsAdapter extends RecyclerView.Adapter {

    List<UMoneyDetailsModel.DataBean> mDataBeanList;
    private Context mContext;

    public UMoneyDetailsAdapter(Context context) {
        mContext = context;
    }

    public void setDataBeanList(List<UMoneyDetailsModel.DataBean> dataBeanList) {
        mDataBeanList = dataBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_umoney_details, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        if (mDataBeanList != null) {
            return mDataBeanList.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_detail)
        TextView mTvDetail;
        @BindView(R.id.view_line)
        View mView;
        @BindView(R.id.tv_time)
        TextView mTvTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(int position) {
            UMoneyDetailsModel.DataBean dataBean = mDataBeanList.get(position);

            // U币个数
            int coinCount = Integer.parseInt(dataBean.getCoinCount());
            // 运动个数
            String sportCount = dataBean.getSportCount();
            // 运动名称
            String sportName = dataBean.getSportName();

            String text = " 个";

            if ("步行".equals(sportName)) {
                text = " 步";
            } else if ("平板支撑".equals(sportName)) {
                text = " 秒";
            }

            if (position == mDataBeanList.size() - 1) {
                mView.setVisibility(View.GONE);
            } else {
                mView.setVisibility(View.VISIBLE);
            }

            SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
            stringBuilder.append(sportName + " " + sportCount + text);
            stringBuilder.append(setSpan(" " + coinCount + " "));
            stringBuilder.append("U币");
            mTvDetail.setText(stringBuilder);

            long create_time = dataBean.getCreate_time();
            String date = TimeUtils.unixTimeToBeijingDate(create_time );

            mTvTime.setText(date);
        }

        public SpannableString setSpan(String what) {
            SpannableString spannableString = new SpannableString(what);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.progrss_bg)), 0, what.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(1.1f), 0, what.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        }


    }
}
